{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4b5616df",
   "metadata": {},
   "source": [
    "## 一、讲述数组叠加、拼合等"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "19a8037c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "729220bf",
   "metadata": {},
   "source": [
    "1. 垂直方向的叠加,使用np.vstack方法。将数字在垂直方向叠加。将数据加在列数据后面，增加的方向判断是横向还是纵向。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "3f774aa3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 6 5 0 1]\n",
      " [7 5 4 2 6]\n",
      " [8 2 4 7 7]]\n",
      "********************\n",
      "[[5 8 3 7 5]]\n"
     ]
    }
   ],
   "source": [
    "vstack1=np.random.randint(0,9,size=(3,5))\n",
    "print(vstack1)\n",
    "print('*'*20)\n",
    "vstack2=np.random.randint(0,9,size=(1,5))\n",
    "print(vstack2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "9c372821",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 6, 5, 0, 1],\n",
       "       [7, 5, 4, 2, 6],\n",
       "       [8, 2, 4, 7, 7],\n",
       "       [5, 8, 3, 7, 5]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vstack3=np.vstack([vstack1,vstack2])\n",
    "vstack3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "c19b7d61",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 6, 5, 0, 1],\n",
       "       [7, 5, 4, 2, 6],\n",
       "       [8, 2, 4, 7, 7],\n",
       "       [5, 8, 3, 7, 5]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v4=np.concatenate([vstack1,vstack2],0)\n",
    "v4"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a41fb43f",
   "metadata": {},
   "source": [
    "2. 使用hstack方法，将数组按照水平方向叠加。即将数据加在每列数据的水平方向上。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "bfade6be",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[6 2 7 1 6]\n",
      " [0 4 8 8 6]\n",
      " [0 1 6 2 1]]\n",
      "********************\n",
      "[[1]\n",
      " [2]\n",
      " [5]]\n"
     ]
    }
   ],
   "source": [
    "hstack1=np.random.randint(0,9,size=(3,5))\n",
    "print(hstack1)\n",
    "print('*'*20)\n",
    "hstack2=np.random.randint(0,9,size=(3,1))\n",
    "print(hstack2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "017d377a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 2, 7, 1, 6, 1],\n",
       "       [0, 4, 8, 8, 6, 2],\n",
       "       [0, 1, 6, 2, 1, 5]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hstack3 = np.hstack([hstack1,hstack2])\n",
    "hstack3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "a69a99eb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 2, 7, 1, 6, 1],\n",
       "       [0, 4, 8, 8, 6, 2],\n",
       "       [0, 1, 6, 2, 1, 5]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "h4=np.concatenate([hstack1,hstack2],1)\n",
    "h4"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c1b9546",
   "metadata": {},
   "source": [
    "3. concatenate([], axis)，手动指定行列的数组叠加。axis是指定行还是列。0为垂直方向叠加（加行），1为水平方向上的叠加（加列）。\n",
    "两种情况与vstack和hstack等效。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "98312755",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'np' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m/tmp/ipykernel_25090/3135698587.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;31m# concatenate中axis参数的特殊情形:none时，即不指定横向还是纵向，那么会拼接成一维数组。\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mh5\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mconcatenate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mhstack1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mhstack2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      3\u001b[0m \u001b[0mh5\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mNameError\u001b[0m: name 'np' is not defined"
     ]
    }
   ],
   "source": [
    "# concatenate中axis参数的特殊情形:none时，即不指定横向还是纵向，那么会拼接成一维数组。\n",
    "h5=np.concatenate([hstack1,hstack2],None)\n",
    "h5"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c23159f",
   "metadata": {},
   "source": [
    "## 二、numpy数组的切割"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b6fbd7a",
   "metadata": {},
   "source": [
    "1. hspilt方法切割，水平切割(减少的方向判断，或者切割刀的排列方向上)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "838385d9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11],\n",
       "       [12, 13, 14, 15]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hs1 = np.arange(16).reshape(4,4)\n",
    "hs1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7c99b2e8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[ 0,  1],\n",
       "        [ 4,  5],\n",
       "        [ 8,  9],\n",
       "        [12, 13]]),\n",
       " array([[ 2,  3],\n",
       "        [ 6,  7],\n",
       "        [10, 11],\n",
       "        [14, 15]])]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#第二个参数是切割出来的数量，必须能切割出来整数，否则报错\n",
    "np.hsplit(hs1,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "80fe0d74",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11],\n",
       "       [12, 13, 14, 15]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#切割行为不改变原有的数组\n",
    "hs1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9bd7a715",
   "metadata": {},
   "outputs": [],
   "source": [
    "#指定切割点，需要是第二个参数是元组，元组中包含切割点"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "6c75f0a5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[ 0],\n",
       "        [ 4],\n",
       "        [ 8],\n",
       "        [12]]),\n",
       " array([[ 1,  2],\n",
       "        [ 5,  6],\n",
       "        [ 9, 10],\n",
       "        [13, 14]]),\n",
       " array([[ 3],\n",
       "        [ 7],\n",
       "        [11],\n",
       "        [15]])]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#0列中间切割，1-3列中间切割。列标含前不含后。\n",
    "np.hsplit(hs1,(1,3))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9e263aeb",
   "metadata": {},
   "source": [
    "2. vsplit(ndarray,splitnum)方法纵向切割，也需要splitnum参数能够被总行数整除。切割减少的数组判断方向(或者切割刀的排列方向上判断)。\n",
    "或者splitnum为元组标识行号。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "34fa6203",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 5, 8],\n",
       "       [8, 2, 6],\n",
       "       [6, 1, 5],\n",
       "       [2, 0, 0],\n",
       "       [6, 2, 2],\n",
       "       [3, 1, 0]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vs1=np.random.randint(0,9,size=(6,3))\n",
    "vs1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "0365616c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[4, 5, 8],\n",
       "        [8, 2, 6]]),\n",
       " array([[6, 1, 5],\n",
       "        [2, 0, 0]]),\n",
       " array([[6, 2, 2],\n",
       "        [3, 1, 0]])]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vsplit(vs1,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "cc6f8ded",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[4, 5, 8]]),\n",
       " array([[8, 2, 6],\n",
       "        [6, 1, 5],\n",
       "        [2, 0, 0]]),\n",
       " array([[6, 2, 2],\n",
       "        [3, 1, 0]])]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#元组标识的行号同样含前不含后。\n",
    "np.vsplit(vs1,(1,4))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ee61bdf4",
   "metadata": {},
   "source": [
    "3. split/array_split,手动参数输入后区分行列切割"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "3851b096",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[ 0,  1],\n",
       "        [ 4,  5],\n",
       "        [ 8,  9],\n",
       "        [12, 13]]),\n",
       " array([[ 2,  3],\n",
       "        [ 6,  7],\n",
       "        [10, 11],\n",
       "        [14, 15]])]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#参数1属于横向切割(刀排列的方向)\n",
    "np.split(hs1,2,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "55328a7c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[ 0],\n",
       "        [ 4],\n",
       "        [ 8],\n",
       "        [12]]),\n",
       " array([[ 1,  2],\n",
       "        [ 5,  6],\n",
       "        [ 9, 10],\n",
       "        [13, 14]]),\n",
       " array([[ 3],\n",
       "        [ 7],\n",
       "        [11],\n",
       "        [15]])]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#最后一个参数1属于横向切割(刀排列的方向),切割数量也可元组参数切割\n",
    "np.split(hs1,(1,3),1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "89b155b5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[0, 1, 2, 3],\n",
       "        [4, 5, 6, 7]]),\n",
       " array([[ 8,  9, 10, 11],\n",
       "        [12, 13, 14, 15]])]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#最后一个参数0属于纵向切割(刀排列的方向)\n",
    "np.split(hs1,2,0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f30ca39d",
   "metadata": {},
   "source": [
    "## 三、数组的转置，行变列，列变行"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2cd17e57",
   "metadata": {},
   "source": [
    "1. 使用T方法进行转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "d0e29928",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 4, 1, 8, 3],\n",
       "       [5, 7, 8, 8, 2],\n",
       "       [2, 5, 4, 2, 8]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t1 = np.random.randint(0,9,size=(3,5))\n",
    "t1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "ed0db20f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 5, 2],\n",
       "       [4, 7, 5],\n",
       "       [1, 8, 4],\n",
       "       [8, 8, 2],\n",
       "       [3, 2, 8]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t1.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "efa8572f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 4, 1, 8, 3],\n",
       "       [5, 7, 8, 8, 2],\n",
       "       [2, 5, 4, 2, 8]])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#转置不改变原数组\n",
    "t1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8ac19852",
   "metadata": {},
   "outputs": [],
   "source": [
    "2. 使用transpose进行转置。可以返回新的数组，这个数组属于视图(View)，修改后改变原来的数组本身。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "67286227",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 1, 4, 5],\n",
       "       [7, 2, 3, 3]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t2 = np.random.randint(0,9,size=(2,4))\n",
    "t2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "59455621",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 7],\n",
       "       [1, 2],\n",
       "       [4, 3],\n",
       "       [5, 3]])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t3=t2.transpose()\n",
    "t3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "0a9e86e5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[100   7]\n",
      " [  1   2]\n",
      " [  4   3]\n",
      " [  5   3]]\n",
      "********************\n",
      "[[100   1   4   5]\n",
      " [  7   2   3   3]]\n"
     ]
    }
   ],
   "source": [
    "t3[0,0]=100\n",
    "print(t3)\n",
    "print('*'*20)\n",
    "print(t2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4b3dc87f",
   "metadata": {},
   "source": [
    "### 矩阵转置的方法总结\n",
    "1. 可以使用ndarray.T或者是ndarrat.transpose()进行\n",
    "2. ndarray.T转置方法，不改变原有的数组，同时变换之后的修改单个元素也不改变原来的数组。\n",
    "3. ndarray.transpose()方法属于一种视图，本身转置完成后不改变原有数组。修改单个元素会修改原有的数组。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
